/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package freya.dao;
import freya.util.*;

import freya.model.Laboratorio;
import freya.util.HibernateUtil;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Danilo Rodrigues
 */
public class LaboratorioDAO implements InterfaceLaboratorioDAO{
    
    Log log_lab = new Log();
    
    private final Session session;
    
    public LaboratorioDAO(){
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    @Override
    public void Insert(Laboratorio laboratorio) {
        try
        {
        Transaction tx_laboratorio = (Transaction) session.beginTransaction();
        session.save(laboratorio);  
        tx_laboratorio.commit();
        log_lab.cadastroSucessSala();
        }
        catch(Exception e) {
            e.printStackTrace();
            log_lab.erroSalaCadastro();
        }
    }

    @Override
    public java.util.List List() {
        List<Laboratorio> list = null;
        Transaction tx_laboratorio = null;
        try
        {
            tx_laboratorio = (Transaction) session.beginTransaction();
            list = (List<Laboratorio>)session.createQuery("from Laboratorio").list();
            for(Laboratorio laboratorio : list)
                Hibernate.initialize(laboratorio.getSala());
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }finally{
            tx_laboratorio.commit();
        }
        return list;
    }
    
}
